using System.Data;
using AnhAnh;
using AutoSales;
using FacebookLib;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Entity;
using System.Drawing;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Resources;
using System.Windows.Forms;

public sealed class FormPost : Form
{
	private FormMain formMain_0;

	private int int_0;

	private IContainer icontainer_0;

	private TextBox textBoxMessage;

	private Label label2;

	private Label label5;

	private TextBox textBoxLink;

	private Button buttonPicture;

	private Label label3;

	private TextBox textBoxPicture;

	private Button buttonSave;

	private OpenFileDialog openFileDialog_0;

	private TreeView treeView1;

	private Label label1;

	private Label label8;

	private CheckBox checkBoxDeleteOldPost;

	private CheckBox checkBoxStopPost;

	private ComboBox comboBoxStartTime;

	public FormPost(FormMain formMain_1)
	{
		this.InitializeComponent();
		this.formMain_0 = formMain_1;
	}

	public FormPost(FormMain formMain_1, int int_1)
	{
		this.InitializeComponent();
		this.formMain_0 = formMain_1;
		this.int_0 = int_1;
	}

	private void buttonPicture_Click(object sender, EventArgs e)
	{
		this.openFileDialog_0.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png, *.gif, *.bmp) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png; *.gif; *.bmp";
		this.openFileDialog_0.InitialDirectory = string.Empty;
		this.openFileDialog_0.FilterIndex = 1;
		if (this.openFileDialog_0.ShowDialog() == System.Windows.Forms.DialogResult.OK)
		{
			string fileName = this.openFileDialog_0.FileName;
			if (!string.IsNullOrEmpty(fileName))
			{
				this.textBoxPicture.Text = fileName;
			}
		}
	}

	private void buttonSave_Click(object sender, EventArgs e)
	{
		if (!this.method_1())
		{
			return;
		}
		Post post = new Post();
		this.method_2(post);
		if (!UI.ValidateUserEntry(post))
		{
			return;
		}
		AutoSalesContext autoSalesContext = new AutoSalesContext();
		List<Group> groups = this.method_0();
		this.method_3(groups, autoSalesContext);
		if (this.int_0 != 0)
		{
			var posts = autoSalesContext.Posts;
			object[] int0 = new object[] { this.int_0 };
			Post post1 = posts.Find(int0);
			this.method_2(post1);
			post1.Groups.Clear();
			post1.Groups=groups;
			autoSalesContext.Entry<Post>(post1).State = EntityState.Modified;
			autoSalesContext.SaveChanges();
			this.formMain_0.RefreshData(post1.Id, true);
		}
		else
		{
			post.Groups=(groups);
			autoSalesContext.Posts.Add(post);
			autoSalesContext.SaveChanges();
			this.formMain_0.RefreshData(post.Id, false);
		}
		base.Close();
	}

	protected override void Dispose(bool bool_0)
	{
		if (bool_0 && this.icontainer_0 != null)
		{
			this.icontainer_0.Dispose();
		}
		base.Dispose(bool_0);
	}

	private void FormPost_Shown(object sender, EventArgs e)
	{
		this.Cursor = Cursors.WaitCursor;
		Application.DoEvents();
		DateTime now = DateTime.Now;
		this.comboBoxStartTime.Items.Add(string.Format("{0:HH:mm}", now));
		for (int i = 7; i < 19; i++)
		{
			this.comboBoxStartTime.Items.Add(string.Format("{0:d}:00", i));
		}
		if (this.int_0 == 0)
		{
			this.comboBoxStartTime.SelectedIndex = 0;
		}
		AutoSalesContext autoSalesContext = new AutoSalesContext();
		DbSet<Category> categories = autoSalesContext.Categories;
		ParameterExpression parameterExpression = Expression.Parameter(typeof(Category), "m");
		MemberExpression memberExpression = Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(Category).GetMethod("get_Title").MethodHandle));
		ParameterExpression[] parameterExpressionArray = new ParameterExpression[] { parameterExpression };
		foreach (Category category in categories.OrderBy<Category, string>(Expression.Lambda<Func<Category, string>>(memberExpression, parameterExpressionArray)))
		{
			TreeNodeCollection nodes = this.treeView1.Nodes[0].Nodes;
			int id = category.Id;
			nodes.Add(id.ToString(), category.Title);
		}
		foreach (GroupData mGroup in this.formMain_0.mGroups)
		{
			FormPost.Class4 class4 = new FormPost.Class4()
			{
				string_0 = mGroup.id
			};
			DbSet<Group> groups = autoSalesContext.Groups;
			ParameterExpression parameterExpression1 = Expression.Parameter(typeof(Group), "m");
			int? nullable = null;
			BinaryExpression binaryExpression = Expression.AndAlso(Expression.Equal(Expression.Property(parameterExpression1, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(Group).GetMethod("get_FacebookId").MethodHandle)), Expression.Field(Expression.Constant(class4), FieldInfo.GetFieldFromHandle(typeof(FormPost.Class4).GetField("string_0").FieldHandle)), false, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(string).GetMethod("op_Equality", new Type[] { typeof(string), typeof(string) }).MethodHandle)), Expression.NotEqual(Expression.Property(parameterExpression1, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(Group).GetMethod("get_CategoryId").MethodHandle)), Expression.Constant(nullable, typeof(int?))));
			ParameterExpression[] parameterExpressionArray1 = new ParameterExpression[] { parameterExpression1 };
			Group group = groups.Where<Group>(Expression.Lambda<Func<Group, bool>>(binaryExpression, parameterExpressionArray1)).FirstOrDefault<Group>();
			if (group == null)
			{
				this.treeView1.Nodes[0].Nodes[0].Nodes.Add(mGroup.id, mGroup.name);
			}
			else
			{
				TreeNodeCollection treeNodeCollections = this.treeView1.Nodes[0].Nodes;
				int? categoryId = group.CategoryId;
				treeNodeCollections[categoryId.ToString()].Nodes.Add(group.FacebookId, group.Name);
			}
		}
		if (this.int_0 > 0)
		{
			DbSet<Post> posts = autoSalesContext.Posts;
			object[] int0 = new object[] { this.int_0 };
			Post post = posts.Find(int0);
			if (post != null)
			{
				this.textBoxMessage.Text = post.Message;
				this.textBoxPicture.Text = post.Picture;
				this.textBoxLink.Text = post.Link;
				this.comboBoxStartTime.Text = post.StartTime;
				this.checkBoxDeleteOldPost.Checked = post.DeleteOldPost;
				this.checkBoxStopPost.Checked = post.StopPost;
				foreach (Group group1 in post.Groups)
				{
					TreeNode[] treeNodeArray = this.treeView1.Nodes.Find(group1.FacebookId, true);
					if ((int)treeNodeArray.Length <= 0)
					{
						continue;
					}
					treeNodeArray[0].Checked = true;
				}
			}
		}
		foreach (TreeNode node in this.treeView1.Nodes[0].Nodes)
		{
			TreeNode treeNode = node;
			object text = treeNode.Text;
			object[] count = new object[] { text, " (", node.Nodes.Count, ")" };
			treeNode.Text = string.Concat(count);
		}
		if (this.treeView1.Nodes[0].Nodes.Count != 1)
		{
			this.treeView1.Nodes[0].Expand();
		}
		else
		{
			this.treeView1.ExpandAll();
		}
		UI.TreeNodeIsCheckAll(this.treeView1.Nodes[0]);
		this.Cursor = Cursors.Default;
		Application.DoEvents();
	}

	private void InitializeComponent()
	{
		TreeNode treeNode = new TreeNode("Uncategorized");
		TreeNode treeNode1 = new TreeNode("All", new TreeNode[] { treeNode });
		ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof(FormPost));
		this.textBoxMessage = new TextBox();
		this.label2 = new Label();
		this.label5 = new Label();
		this.textBoxLink = new TextBox();
		this.buttonPicture = new Button();
		this.label3 = new Label();
		this.textBoxPicture = new TextBox();
		this.buttonSave = new Button();
		this.openFileDialog_0 = new OpenFileDialog();
		this.treeView1 = new TreeView();
		this.label1 = new Label();
		this.label8 = new Label();
		this.checkBoxDeleteOldPost = new CheckBox();
		this.checkBoxStopPost = new CheckBox();
		this.comboBoxStartTime = new ComboBox();
		base.SuspendLayout();
		this.textBoxMessage.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left;
		this.textBoxMessage.Location = new Point(12, 28);
		this.textBoxMessage.Multiline = true;
		this.textBoxMessage.Name = "textBoxMessage";
		this.textBoxMessage.ScrollBars = ScrollBars.Both;
		this.textBoxMessage.Size = new System.Drawing.Size(370, 246);
		this.textBoxMessage.TabIndex = 0;
		this.label2.AutoSize = true;
		this.label2.Location = new Point(12, 12);
		this.label2.Name = "label2";
		this.label2.Size = new System.Drawing.Size(50, 13);
		this.label2.TabIndex = 9;
		this.label2.Text = "Message";
		this.label5.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
		this.label5.AutoSize = true;
		this.label5.Location = new Point(12, 316);
		this.label5.Name = "label5";
		this.label5.Size = new System.Drawing.Size(27, 13);
		this.label5.TabIndex = 39;
		this.label5.Text = "Link";
		this.textBoxLink.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
		this.textBoxLink.Location = new Point(70, 312);
		this.textBoxLink.Name = "textBoxLink";
		this.textBoxLink.Size = new System.Drawing.Size(312, 20);
		this.textBoxLink.TabIndex = 3;
		this.buttonPicture.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
		this.buttonPicture.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
		this.buttonPicture.Location = new Point(342, 280);
		this.buttonPicture.Name = "buttonPicture";
		this.buttonPicture.Size = new System.Drawing.Size(40, 23);
		this.buttonPicture.TabIndex = 2;
		this.buttonPicture.Text = "...";
		this.buttonPicture.UseVisualStyleBackColor = true;
		this.buttonPicture.Click += new EventHandler(this.buttonPicture_Click);
		this.label3.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
		this.label3.AutoSize = true;
		this.label3.Location = new Point(12, 286);
		this.label3.Name = "label3";
		this.label3.Size = new System.Drawing.Size(40, 13);
		this.label3.TabIndex = 37;
		this.label3.Text = "Picture";
		this.textBoxPicture.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
		this.textBoxPicture.BackColor = SystemColors.Window;
		this.textBoxPicture.Location = new Point(70, 282);
		this.textBoxPicture.Name = "textBoxPicture";
		this.textBoxPicture.Size = new System.Drawing.Size(266, 20);
		this.textBoxPicture.TabIndex = 1;
		this.buttonSave.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
		this.buttonSave.Location = new Point(587, 369);
		this.buttonSave.Name = "buttonSave";
		this.buttonSave.Size = new System.Drawing.Size(85, 35);
		this.buttonSave.TabIndex = 8;
		this.buttonSave.Text = "Save";
		this.buttonSave.UseVisualStyleBackColor = true;
		this.buttonSave.Click += new EventHandler(this.buttonSave_Click);
		this.treeView1.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
		this.treeView1.CheckBoxes = true;
		this.treeView1.Location = new Point(388, 28);
		this.treeView1.Name = "treeView1";
		treeNode.Name = "NodeUncategorized";
		treeNode.Text = "Uncategorized";
		treeNode1.Name = "NodeAll";
		treeNode1.Text = "All";
		this.treeView1.Nodes.AddRange(new TreeNode[] { treeNode1 });
		this.treeView1.Size = new System.Drawing.Size(284, 335);
		this.treeView1.TabIndex = 7;
		this.treeView1.AfterCheck += new TreeViewEventHandler(this.treeView1_AfterCheck);
		this.label1.AutoSize = true;
		this.label1.Location = new Point(388, 12);
		this.label1.Name = "label1";
		this.label1.Size = new System.Drawing.Size(87, 13);
		this.label1.TabIndex = 45;
		this.label1.Text = "Facebook Group";
		this.label8.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
		this.label8.AutoSize = true;
		this.label8.Location = new Point(12, 346);
		this.label8.Name = "label8";
		this.label8.Size = new System.Drawing.Size(55, 13);
		this.label8.TabIndex = 47;
		this.label8.Text = "Start Time";
		this.checkBoxDeleteOldPost.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
		this.checkBoxDeleteOldPost.AutoSize = true;
		this.checkBoxDeleteOldPost.Location = new Point(165, 344);
		this.checkBoxDeleteOldPost.Name = "checkBoxDeleteOldPost";
		this.checkBoxDeleteOldPost.Size = new System.Drawing.Size(97, 17);
		this.checkBoxDeleteOldPost.TabIndex = 5;
		this.checkBoxDeleteOldPost.Text = "Delete old post";
		this.checkBoxDeleteOldPost.UseVisualStyleBackColor = true;
		this.checkBoxStopPost.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
		this.checkBoxStopPost.AutoSize = true;
		this.checkBoxStopPost.Location = new Point(278, 344);
		this.checkBoxStopPost.Name = "checkBoxStopPost";
		this.checkBoxStopPost.Size = new System.Drawing.Size(90, 17);
		this.checkBoxStopPost.TabIndex = 6;
		this.checkBoxStopPost.Text = "Stop this post";
		this.checkBoxStopPost.UseVisualStyleBackColor = true;
		this.comboBoxStartTime.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
		this.comboBoxStartTime.FormattingEnabled = true;
		this.comboBoxStartTime.Location = new Point(70, 342);
		this.comboBoxStartTime.Name = "comboBoxStartTime";
		this.comboBoxStartTime.Size = new System.Drawing.Size(67, 21);
		this.comboBoxStartTime.TabIndex = 4;
		base.AutoScaleDimensions = new SizeF(6f, 13f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.ClientSize = new System.Drawing.Size(684, 412);
		base.Controls.Add(this.comboBoxStartTime);
		base.Controls.Add(this.checkBoxDeleteOldPost);
		base.Controls.Add(this.checkBoxStopPost);
		base.Controls.Add(this.label8);
		base.Controls.Add(this.label1);
		base.Controls.Add(this.treeView1);
		base.Controls.Add(this.buttonSave);
		base.Controls.Add(this.label5);
		base.Controls.Add(this.textBoxLink);
		base.Controls.Add(this.buttonPicture);
		base.Controls.Add(this.label3);
		base.Controls.Add(this.textBoxPicture);
		base.Controls.Add(this.label2);
		base.Controls.Add(this.textBoxMessage);
//		base.Icon = (System.Drawing.Icon)componentResourceManager.GetObject("$this.Icon");
		base.Name = "FormPost";
		base.StartPosition = FormStartPosition.CenterParent;
		this.Text = "Post Ads";
		base.Shown += new EventHandler(this.FormPost_Shown);
		base.ResumeLayout(false);
		base.PerformLayout();
	}

	private List<Group> method_0()
	{
		List<Group> groups = new List<Group>();
		foreach (TreeNode node in this.treeView1.Nodes[0].Nodes)
		{
			foreach (TreeNode treeNode in node.Nodes)
			{
				if (treeNode.Level != 2 || !treeNode.Checked)
				{
					continue;
				}
				Group group = new Group();
				group.FacebookId=(treeNode.Name);
				group.Name=(treeNode.Text);
				groups.Add(group);
			}
		}
		return groups.Distinct<Group>().ToList<Group>();
	}

	private bool method_1()
	{
		if (this.textBoxMessage.Text == string.Empty && this.textBoxLink.Text == string.Empty && this.textBoxPicture.Text == string.Empty)
		{
			UI.ShowMessage("You must enter a post value", "Invalid entry", UI.MessageType.Error);
			return false;
		}
		if (string.IsNullOrEmpty(this.comboBoxStartTime.Text))
		{
			UI.ShowMessage("You must enter start time value", "Invalid entry", UI.MessageType.Error);
			return false;
		}
		if (FormMain.mAnh.CheckTimeFormat(this.comboBoxStartTime.Text) && FormMain.mAnh.CheckTimeValue(this.comboBoxStartTime.Text))
		{
			return true;
		}
		UI.ShowMessage("Not a valid time format ('hh:mm')", "Invalid entry", UI.MessageType.Error);
		this.comboBoxStartTime.Focus();
		return false;
	}

	private void method_2(Post post_0)
	{
		post_0.Message=(this.textBoxMessage.Text.Trim());
		post_0.Picture=(this.textBoxPicture.Text.Trim());
		post_0.Link=(this.textBoxLink.Text.Trim());
		post_0.StartTime=(this.comboBoxStartTime.Text);
		post_0.DeleteOldPost=(this.checkBoxDeleteOldPost.Checked);
		post_0.StopPost=(this.checkBoxStopPost.Checked);
		if (!string.IsNullOrEmpty(this.textBoxPicture.Text))
		{
			post_0.Type=("photo");
			return;
		}
		if (!string.IsNullOrEmpty(this.textBoxLink.Text))
		{
			post_0.Type=("link");
			return;
		}
		post_0.Type=("status");
	}

	private void method_3(List<Group> list_0, AutoSalesContext autoSalesContext_0)
	{
		for (int i = 0; i < list_0.Count; i++)
		{
			FormPost.Class3 class3 = new FormPost.Class3()
			{
				string_0 = list_0[i].FacebookId
			};
			DbSet<Group> groups = autoSalesContext_0.Groups;
			ParameterExpression parameterExpression = Expression.Parameter(typeof(Group), "m");
			BinaryExpression binaryExpression = Expression.Equal(Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(Group).GetMethod("get_FacebookId").MethodHandle)), Expression.Field(Expression.Constant(class3), FieldInfo.GetFieldFromHandle(typeof(FormPost.Class3).GetField("string_0").FieldHandle)), false, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(string).GetMethod("op_Equality", new Type[] { typeof(string), typeof(string) }).MethodHandle));
			ParameterExpression[] parameterExpressionArray = new ParameterExpression[] { parameterExpression };
			Group group = groups.Where<Group>(Expression.Lambda<Func<Group, bool>>(binaryExpression, parameterExpressionArray)).FirstOrDefault<Group>();
			if (group != null)
			{
				list_0[i] = group;
			}
		}
	}

	private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
	{
		foreach (TreeNode node in e.Node.Nodes)
		{
			node.Checked = e.Node.Checked;
		}
	}

	private sealed class Class3
	{
		public string string_0;

		public Class3()
		{
		}
	}

	private sealed class Class4
	{
		public string string_0;

		public Class4()
		{
		}
	}
}